// pages/register/index.js
import {
  getRegisterCodeApi,
  postResetPasswordApi
} from '../../../https/user'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 顶部标题栏动态设置
    topBarInfo: {
      navBarHeight: app.globalData.navBarHeight,
      menuRight: app.globalData.menuRight,
      menuTop: app.globalData.menuTop,
      menuHeight: app.globalData.menuHeight,
    },
    email: "",
    code: "",
    password: "",
    rePassword: "",
    auth: false,
    codeNum: 0,
    isLoad: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  // 获取验证码
  async getCode() {
    const {
      codeNum,
      email,
      isLoad
    } = this.data
    console.log(email)
    if (!this.isEmail(email)) {
      wx.showToast({
        title: '请输入正确的邮箱格式',
        icon: 'none'
      })
      return
    }

    if (isLoad) return
    if (codeNum != 0) return

    this.setData({
      isLoad: true
    })

    // 获取验证码
    const getRes = await getRegisterCodeApi({
      email
    })
    if (getRes.code == 200) {
      wx.showToast({
        title: '验证码已发送',
        icon: 'none'
      })
      this.setData({
        codeNum: 20
      })
      // 开启定时器
      const timer = setInterval(() => {
        console.log(this.data.codeNum)
        const number = this.data.codeNum
        if (number == 0) {
          clearInterval(timer)
          return
        }
        this.setData({
          codeNum: number - 1
        })
      }, 1000);
    } else {
      wx.showToast({
        title: '验证码发送失败',
        icon: "error"
      })
    }
    console.log(getRes)
    this.setData({
      isLoad: false
    })
  },
  // input输入框
  changeInput(e) {
    const value = e.detail.value
    const type = e.currentTarget.dataset.type
    this.setData({
      [type]: value
    })
    console.log(this.data)
  },
  // 切换授权
  toggleAuth() {
    this.setData({
      auth: !this.data.auth
    })
  },
  // 邮箱格式验证
  isEmail(email) {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(email);
  },
  // 注册按钮
  async registerBtn() {
    const {
      email,
      code,
      password,
      rePassword,
      auth
    } = this.data
    if (!this.isEmail(email)) {
      wx.showToast({
        title: '请输入正确的邮箱',
        icon: 'none'
      })
      return
    }
    if (code.length != 6) {
      wx.showToast({
        title: '验证码为6位字符',
        icon: 'none'
      })
      return
    }
    if (password != rePassword) {
      wx.showToast({
        title: '两次密码不一致',
        icon: 'none'
      })
      return
    }
    if (password.length > 16 || password.length < 8) {
      wx.showToast({
        title: '密码长度为8-16位字符',
        icon: 'none'
      })
      return
    }
    if (!auth) {
      wx.showToast({
        title: '请同意注册隐私协议',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '注册中..',
      mask: true,

    })
    const postRes = await postResetPasswordApi({
      email,
      password,
      code
    })
    console.log(postRes)
    if (postRes.code == 200) {
      wx.showToast({
        title: '重置成功',
        icon: 'success',
        mask: true
      })
      setTimeout(() => {
        wx.redirectTo({
          url: '../login/index',
        })
      }, 1000)
    } else {
      wx.showToast({
        title: postRes.message,
        icon: 'none'
      })
      return
    }
  },
  // 放回上一页
  back() {
    wx.navigateBack()
  }

})